package com.example.fragmentdemo.helper;

import static androidx.constraintlayout.helper.widget.MotionEffect.TAG;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.text.TextUtils;
import android.util.Log;

import androidx.annotation.Nullable;


import com.example.fragmentdemo.bean.FileBean;
import com.example.fragmentdemo.constant.Constant;

import java.util.ArrayList;


public class SQLHelper extends SQLiteOpenHelper {

    private static SQLHelper instance;

    public SQLHelper(Context context) {
        super(context, Constant.DB_NAME, null, 1);
    }

    public SQLHelper(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
    }
    public static synchronized SQLHelper getInstance(Context context) {
        if (instance == null) {
            instance = new SQLHelper(context);
        }
        return instance;
    }
    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(Constant.CREATE_TABLE_SQL);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }

    public void insertFileData(ArrayList<FileBean> beanList) {
        SQLiteDatabase writableDatabase = getWritableDatabase();
        try {
            writableDatabase.beginTransaction(); // 开启事务
            ContentValues contentValues = new ContentValues();
            for (FileBean fileBean : beanList) {
                contentValues.put("name",fileBean.getName());
                contentValues.put("path",fileBean.getPath());
                contentValues.put("createTime",fileBean.getCreateTime());
                contentValues.put("updateTime",fileBean.getUpdateTime());
                contentValues.put("time",fileBean.getTime());
                contentValues.put("length",fileBean.getLength());
                contentValues.put("fileType",fileBean.getFileType());
                writableDatabase.insert(Constant.TABLE_NAME,null,contentValues);
            }
            writableDatabase.setTransactionSuccessful(); // 标记事务成功
        } catch (Exception e) {
            Log.d(TAG, "insertFileData: "+e.getMessage());
        } finally {
            writableDatabase.endTransaction(); // 结束事务
        }
    }

    @SuppressLint("Range")
    public ArrayList<FileBean> queryFileByType(String fileType) {
        SQLiteDatabase db = this.getReadableDatabase();
        ArrayList<FileBean> fileList = new ArrayList<>();
        Cursor cursor = db.query(Constant.TABLE_NAME, null, "fileType = ?", new String[]{fileType}, null, null, null);
        if (cursor != null) {
            while (cursor.moveToNext()) {
                FileBean fileBean = new FileBean();
                fileBean.setName(cursor.getString(cursor.getColumnIndex("name")));
                fileBean.setPath(cursor.getString(cursor.getColumnIndex("path")));
                fileBean.setLength(cursor.getString(cursor.getColumnIndex("length")));
                fileBean.setCreateTime(cursor.getString(cursor.getColumnIndex("createTime")));
                fileBean.setUpdateTime(cursor.getString(cursor.getColumnIndex("updateTime")));
                fileBean.setFileType(cursor.getString(cursor.getColumnIndex("fileType")));
                // 如果是音频或视频文件，需要设置时长
                if ("AUDIO".equals(fileBean.getFileType()) || "VIDEO".equals(fileBean.getFileType())) {
                    fileBean.setTime(cursor.getString(cursor.getColumnIndex("time")));
                }
                fileList.add(fileBean);
            }
            cursor.close();
        }
        db.close();
        return fileList;
    }

    public boolean hasData() {
        SQLiteDatabase db = getReadableDatabase();
        Cursor cursor = null;

        try {
            // 查询数据库中的记录数
            String query = "SELECT COUNT(*) FROM " + Constant.TABLE_NAME;
            cursor = db.rawQuery(query, null);

            if (cursor.moveToFirst()) {
                int count = cursor.getInt(0);
                return count > 0; // 如果记录数大于0，则表示数据库中已经存在数据
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (cursor != null) {
                cursor.close();
            }
            db.close();
        }

        return false; // 默认返回false，表示数据库中没有数据或查询出错
    }

    @SuppressLint("Range")
    public ArrayList<FileBean> queryFileByName(String name) {

        SQLiteDatabase db = this.getReadableDatabase();
        ArrayList<FileBean> fileList = new ArrayList<>();
        Cursor cursor = db.query(Constant.TABLE_NAME, null, "name = ?", new String[]{"%"+name+"%"}, null, null, null);
        if (cursor != null) {
            while (cursor.moveToNext()) {
                FileBean fileBean = new FileBean();
                fileBean.setName(cursor.getString(cursor.getColumnIndex("name")));
                fileBean.setPath(cursor.getString(cursor.getColumnIndex("path")));
                fileBean.setLength(cursor.getString(cursor.getColumnIndex("length")));
                fileBean.setCreateTime(cursor.getString(cursor.getColumnIndex("createTime")));
                fileBean.setUpdateTime(cursor.getString(cursor.getColumnIndex("updateTime")));
                fileBean.setFileType(cursor.getString(cursor.getColumnIndex("fileType")));
                // 如果是音频或视频文件，需要设置时长
                if ("AUDIO".equals(fileBean.getFileType()) || "VIDEO".equals(fileBean.getFileType())) {
                    fileBean.setTime(cursor.getString(cursor.getColumnIndex("time")));
                }
                fileList.add(fileBean);
            }
            cursor.close();
        }
        db.close();
        return fileList;
    }

    @SuppressLint("Range")
    public ArrayList<FileBean> queryAll() {

        SQLiteDatabase db = this.getReadableDatabase();
        ArrayList<FileBean> fileList = new ArrayList<>();
        Cursor cursor = db.query(Constant.TABLE_NAME, null, null, null, null, null, "createTime");
        if (cursor != null) {
            while (cursor.moveToNext()) {
                FileBean fileBean = new FileBean();
                fileBean.setName(cursor.getString(cursor.getColumnIndex("name")));
                fileBean.setPath(cursor.getString(cursor.getColumnIndex("path")));
                fileBean.setLength(cursor.getString(cursor.getColumnIndex("length")));
                fileBean.setCreateTime(cursor.getString(cursor.getColumnIndex("createTime")));
                fileBean.setUpdateTime(cursor.getString(cursor.getColumnIndex("updateTime")));
                fileBean.setFileType(cursor.getString(cursor.getColumnIndex("fileType")));
                // 如果是音频或视频文件，需要设置时长
                if ("AUDIO".equals(fileBean.getFileType()) || "VIDEO".equals(fileBean.getFileType())) {
                    fileBean.setTime(cursor.getString(cursor.getColumnIndex("time")));
                }
                fileList.add(fileBean);
            }
            cursor.close();
        }
        db.close();
        return fileList;
    }


}
